<?php
declare(strict_types = 1);

use think\facade\Config;
use think\facade\Event;
use think\facade\Route;
use think\helper\{
    Str, Arr
};

\think\Console::starting(function (\think\Console $console) {
    $console->addCommands([
        'addons:config' => '\\think\\addons\\command\\SendConfig'
    ]);
});

// 插件类库自动载入
spl_autoload_register(function ($class) {
    $class     = ltrim($class, '\\');
    $addonsDir = Config::get('addons.dir', 'addons');
    $dir       = app()->getRootPath();
    $namespace = $addonsDir;

    if (strpos($class, $namespace) === 0) {
        $class = substr($class, strlen($namespace));
        $path  = '';
        if (($pos = strripos($class, '\\')) !== false) {
            $path  = str_replace('\\', '/', substr($class, 0, $pos)) . '/';
            $class = substr($class, $pos + 1);
        }
        $path .= str_replace('_', '/', $class) . '.php';
        $dir  .= $namespace . $path;

        if (file_exists($dir)) {
            include $dir;
            return true;
        }
        return false;
    }
    return false;
});

if (!function_exists('hook')) {
    /**
     * 处理插件钩子
     * @param string     $event  钩子名称
     * @param array|null $params 传入参数
     * @param bool       $isArray
     * @param bool       $once   是否只返回一个结果
     * @return mixed
     */
    function hook($event, $params = null, $isArray = false, bool $once = false)
    {
        $result = Event::trigger($event, $params, $once);
        return $isArray ? $result : join('', $result);
    }
}

if (!function_exists('get_addons_info')) {
    /**
     * 读取插件的基础信息
     * @param string $name 插件名
     * @return array
     */
    function get_addons_info($name)
    {
        $addon = get_addons_instance($name);
        if (!$addon) {
            return [];
        }

        return $addon->getInfo();
    }
}

if (!function_exists('get_addons_instance')) {
    /**
     * 获取插件的单例
     * @param string $name 插件名
     * @return mixed|null
     */
    function get_addons_instance($name)
    {
        static $_addons = [];
        if (isset($_addons[ $name ])) {
            return $_addons[ $name ];
        }
        $class = get_addons_class($name);

        if (class_exists($class)) {
            $_addons[ $name ] = new $class(app());
            return $_addons[ $name ];
        } else {
            return null;
        }
    }
}

if (!function_exists('get_addons_class')) {
    /**
     * 获取插件类的类名
     * @param string $name  插件名
     * @param string $type  返回命名空间类型
     * @param string $class 当前类名
     * @return string
     */
    function get_addons_class($name, $type = 'hook', $class = null)
    {
        $name      = trim($name);
        $addonsDir = Config::get('addons.dir', 'addons');
        // 处理多级控制器情况
        if (!is_null($class) && strpos($class, '.')) {
            $class = explode('.', $class);

            $class[ count($class) - 1 ] = Str::studly(end($class));
            $class                      = implode('\\', $class);
        } else {
            $class = Str::studly(is_null($class) ? $name : $class);
        }
        switch ($type) {
            case 'controller':
                $class     = Config::get('route.controller_suffix') ? $class . 'Controller' : $class;
                $namespace = '\\' . $addonsDir . '\\' . $name . '\\controller\\' . $class;
                break;
            default:
                $namespace = '\\' . $addonsDir . '\\' . $name . '\\' . $class;
        }

        return class_exists($namespace) ? $namespace : '';
    }
}

if (!function_exists('addons_url')) {
    /**
     * 插件显示内容里生成访问插件的url
     * @param             $url
     * @param array       $param
     * @param bool|string $suffix 生成的URL后缀
     * @param bool|string $domain 域名
     * @return bool|string
     */
    function addons_url($url = '', $param = [], $suffix = false, $domain = false)
    {
        $request   = app('request');
        $addonsDir = Config::get('addons.dir', 'addons');
        if (empty($url)) {
            // 生成 url 模板变量
            $addons     = $request->addon;
            $controller = $request->controller();
            $controller = str_replace('/', '.', $controller);
            $action     = $request->action();
        } else {
            $url = Str::studly($url);
            $url = parse_url($url);
            if (isset($url['scheme'])) {
                $addons     = strtolower($url['scheme']);
                $controller = $url['host'];
                $action     = trim($url['path'], '/');
            } else {
                $route      = explode('/', $url['path']);
                $addons     = $request->addon;
                $action     = array_pop($route);
                $controller = array_pop($route) ?: $request->controller();
            }
            $controller = Str::snake((string) $controller);

            /* 解析URL带的参数 */
            if (isset($url['query'])) {
                parse_str($url['query'], $query);
                $param = array_merge($query, $param);
            }
        }

        return Route::buildUrl("@{$addonsDir}/{$addons}/{$controller}-{$action}", $param)->suffix($suffix)->domain($domain);
    }
}


function statics_file(string $type = "header")
{
    $request   = app('request');
    $name = $request->param('addon');
    if (empty($request->plugin()) or $request->plugin() !== $name) {
        return false;
    }

    switch ($type) {
        case 'header':
            $content = 'plugin/' . $name . '/style/css/' . $name . '.css';
            break;
        case 'footer':
            $content = <<<EOT
<script>
    layui.extend({
        {$name}: "../../../plugin/{$name}/style/js/{$name}"
    }).use(['{$name}']);
</script>

EOT;
            break;
        default:
            $content = '';
    }
    return $content;
}
